package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.BranchSettingDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.BranchSettingSercive;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class BranchSettingSerciveImpl implements BranchSettingSercive {

    @Autowired
    private BranchSettingDao branchSettingDao;

    //查询分院信息
    @Override
    public PageResult findAll() {
        Page<Address> page = branchSettingDao.findAll();
        return new PageResult(page.getTotal(), page);
    }

    //新增分院信息
    @Override
    public Result submit(Address address) {
        if(address.getId()==null){
            try{
                branchSettingDao.save(address);
                return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
            }catch (Exception e){
                return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
            }
        }else {
            try{
                branchSettingDao.update(address);
                return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
            }catch (Exception e){
                return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
            }
        }
    }

    //根据ID查询分院信息
    @Override
    public Address findById(Integer id) {
        Address address = branchSettingDao.findById(id);

        return address ;
    }

    @Override
    public void deleteById(Integer id) {
        branchSettingDao.deleteById(id);
    }

    @Override
    public void updateEnable(Integer id, Integer enable) {
        branchSettingDao.updateEnable(id,enable);
    }
}
